"""
测试Ini_Utils功能
"""

import os
import tempfile
from com.tools.Ini_Utils import IniUtils


def test_ini_utils():
    """
    测试INI工具类
    """
    # 创建临时目录用于测试
    with tempfile.TemporaryDirectory() as temp_dir:
        print(f"使用临时目录进行测试: {temp_dir}")
        
        # 测试创建空的INI文件
        print("\n测试创建空的INI文件:")
        ini_path = os.path.join(temp_dir, "test.ini")
        ini = IniUtils.create_empty_ini(ini_path)
        
        # 测试添加内容
        print("\n测试添加内容:")
        ini.set_value("database", "host", "localhost")
        ini.set_value("database", "port", 3306)
        ini.set_value("database", "username", "admin")
        ini.set_value("database", "password", "secret")
        
        ini.set_value("general", "debug", True)
        ini.set_value("general", "log_level", "INFO")
        ini.set_value("network", "timeout", 30.5)
        ini.set_value("network", "retries", 3)
        
        # 测试保存INI文件
        print("\n测试保存INI文件:")
        ini.save_ini()
        print(f"INI文件已保存到: {ini_path}")
        
        # 测试加载INI文件
        print("\n测试加载INI文件:")
        ini2 = IniUtils(ini_path)
        
        # 测试获取所有内容
        print("\n测试获取所有内容:")
        all_content = ini2.get_all_content()
        print("所有内容:", all_content)
        assert "database" in all_content
        assert "general" in all_content
        assert "network" in all_content
        
        # 测试获取指定节的内容
        print("\n测试获取指定节的内容:")
        db_content = ini2.get_section("database")
        print("database节内容:", db_content)
        assert "host" in db_content
        assert db_content["host"] == "localhost"
        
        # 测试获取指定键的值
        print("\n测试获取指定键的值:")
        host = ini2.get_value("database", "host", "default_host")
        print("数据库主机:", host)
        assert host == "localhost"
        
        port = ini2.get_int("database", "port", 5432)
        print("数据库端口:", port)
        assert port == 3306
        
        timeout = ini2.get_float("network", "timeout", 60.0)
        print("超时时间:", timeout)
        assert timeout == 30.5
        
        debug = ini2.get_boolean("general", "debug", False)
        print("调试模式:", debug)
        assert debug == True
        
        # 测试不存在的键
        default_host = ini2.get_value("database", "nonexistent", "default_host")
        print("不存在的键的默认值:", default_host)
        assert default_host == "default_host"
        
        # 测试检查方法
        print("\n测试检查方法:")
        assert ini2.has_section("database") == True
        assert ini2.has_section("nonexistent") == False
        assert ini2.has_option("database", "host") == True
        assert ini2.has_option("database", "nonexistent") == False
        
        # 测试获取节和键列表
        sections = ini2.get_sections()
        print("所有节:", sections)
        assert "database" in sections
        assert "general" in sections
        assert "network" in sections
        
        options = ini2.get_options("database")
        print("database节中的键:", options)
        assert "host" in options
        assert "port" in options
        
        # 测试修改内容
        print("\n测试修改内容:")
        ini2.set_value("database", "host", "192.168.1.100")
        modified_host = ini2.get_value("database", "host")
        print("修改后的主机:", modified_host)
        assert modified_host == "192.168.1.100"
        
        # 测试删除操作
        print("\n测试删除操作:")
        # 删除键
        removed = ini2.remove_option("database", "password")
        print("删除password键:", removed)
        assert removed == True
        assert ini2.has_option("database", "password") == False
        
        # 删除节
        removed = ini2.remove_section("network")
        print("删除network节:", removed)
        assert removed == True
        assert ini2.has_section("network") == False
        
        # 清空节
        cleared = ini2.clear_section("general")
        print("清空general节:", cleared)
        assert cleared == True
        assert len(ini2.get_options("general")) == 0
        
        # 保存修改后的文件
        modified_ini_path = os.path.join(temp_dir, "modified.ini")
        ini2.save_ini(modified_ini_path)
        print(f"修改后的INI文件已保存到: {modified_ini_path}")
        
        # 测试从字典创建INI
        print("\n测试从字典创建INI:")
        data = {
            "section1": {
                "key1": "value1",
                "key2": 123
            },
            "section2": {
                "key3": True,
                "key4": 45.6
            }
        }
        ini3 = IniUtils.from_dict(data)
        dict_ini_path = os.path.join(temp_dir, "from_dict.ini")
        ini3.save_ini(dict_ini_path)
        print(f"从字典创建的INI文件已保存到: {dict_ini_path}")
        
        # 验证内容
        loaded_ini3 = IniUtils(dict_ini_path)
        assert loaded_ini3.get_value("section1", "key1") == "value1"
        assert loaded_ini3.get_int("section1", "key2") == 123
        assert loaded_ini3.get_boolean("section2", "key3") == True
        assert loaded_ini3.get_float("section2", "key4") == 45.6
        
        # 测试合并INI文件
        print("\n测试合并INI文件:")
        ini4 = IniUtils(modified_ini_path)
        ini4.merge_ini(dict_ini_path)
        merged_ini_path = os.path.join(temp_dir, "merged.ini")
        ini4.save_ini(merged_ini_path)
        print(f"合并后的INI文件已保存到: {merged_ini_path}")
        
        # 验证合并结果
        merged_ini = IniUtils(merged_ini_path)
        assert merged_ini.has_section("section1") == True
        assert merged_ini.has_section("section2") == True
        assert merged_ini.has_section("database") == True
        assert merged_ini.get_value("section1", "key1") == "value1"
        
        # 测试获取INI文件信息
        print("\n测试获取INI文件信息:")
        info = merged_ini.get_ini_info()
        print("INI文件信息:", info)
        assert info['sections_count'] >= 3
        assert "database" in info['sections']
        assert "section1" in info['sections']
        assert "section2" in info['sections']
        
        print("\n所有测试通过!")


if __name__ == "__main__":
    test_ini_utils()